package com.cy.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.server.dto.EmployeeDto;
import com.cy.server.mapper.MailLogMapper;
import com.cy.server.pojo.Employee;
import com.cy.server.mapper.EmployeeMapper;
import com.cy.server.pojo.MailConstants;
import com.cy.server.pojo.MailLog;
import com.cy.server.pojo.RespBean;
import com.cy.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiShaoZhuo
 * @since 2021-04-26
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    MailLogMapper mailLogMapper;

    /**
     * @Description: 员工信息分页
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/5/6
     */
    @Override
    public PageInfo<Employee> selectEmployee(EmployeeDto employeeDto) {
        PageHelper.startPage(employeeDto.getCurrentPage(), employeeDto.getPageSize());
        List<Employee> list = employeeMapper.selectEmployee(employeeDto);
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(list);
        return pageInfo;
    }

    /**
     * @Description: 获取最大工号
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/5/6
     */
    @Override
    public RespBean selectMaxWorkId() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        return RespBean.success(null, String.format("%08d",Integer.parseInt(maps.get(0).get("max(workID)").toString())+1));
    }

    /**
     * @Description: 添加员工
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/5/7
     */
    @Override
    public RespBean addEmployee(Employee employee) {
        //处理合同期限，保留两位小数
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));
        if (employeeMapper.insert(employee) == 1){
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            //数据库记录发送的消息
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);
            //发送消息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME, emp,
                    new CorrelationData(msgId));
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    /**
     * @Description: 查询员工
     * @Param:
     * @return:
     * @Author: LiShaoZhuo
     * @Date: 2021/5/7
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    @Override
    public PageInfo<Employee> selectEmployeeWithSalary(EmployeeDto employeeDto) {
        PageHelper.startPage(employeeDto.getCurrentPage(),employeeDto.getPageSize());
        List<Employee> list = employeeMapper.selectEmployeeWithSalary(employeeDto);
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(list);
        return pageInfo;
    }
}
